﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class OrderStatusController
    {
        public IList<OrderStatus> GetAllOrderStatus()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<OrderStatus>().ToList();
            }
        }

        public IList<String> GetAllOrderStatusStatus()
        {
            return GetAllOrderStatus().Select(orderStatus => orderStatus.Status).ToList();
        }

        public int AddOrderStatus(OrderStatus orderStatus)
        {
            try
            {
                GetAllOrderStatus()
                    .Where(o => o.Status == orderStatus.Status)
                    .Select(o => o)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(orderStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditOrderStatus(OrderStatus newOrderStatus, String oldStatus)
        {
            try
            {
                var checkOrderStatus = GetAllOrderStatus()
                    .Where(o => o.Status == newOrderStatus.Status)
                    .Select(o => o)
                    .Single();

                if (checkOrderStatus.Status == oldStatus)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newOrderStatus);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newOrderStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteOrderStatus(OrderStatus orderStatus)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(orderStatus);
                    transaction.Commit();
                }
            }
        }
    }
}
